﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.AWSServices.S3;
using System.Diagnostics;
using System.Linq;

namespace UnitTesting
{
    [TestFixture]
    public class S3Testing
    {
        private const string ACCESS_KEY = "01ZWQXTSGN5F9WE8VY02";
        private const string SECRET_KEY = "gHrbPJegTleOjom5dPi8jkWONdWLlNsr+BF+J96w";
        private S3Client m_S3Client;

        [SetUp]
        public void Setup()
        {
            m_S3Client = new S3Client();
        }

        [TearDown]
        public void TearDown()
        {
            m_S3Client.Dispose();
        }


        [Test]
        public void TestConnection()
        {
            m_S3Client = new S3Client(ACCESS_KEY, SECRET_KEY, false);

            // connect
            bool success = m_S3Client.EstablishConnection();

            Assert.IsTrue(success);
            Assert.IsTrue(m_S3Client.IsConnected);

            // disconnect
            m_S3Client.Disconnect();

            Assert.IsFalse(m_S3Client.IsConnected);
        }

        [Test]
        public void TestSSL()
        {
            m_S3Client = new S3Client(ACCESS_KEY, SECRET_KEY);

            // connect
            bool success = m_S3Client.EstablishConnection();

            Assert.IsTrue(success);
            Assert.IsTrue(m_S3Client.IsConnected);

            // disconnect
            m_S3Client.Disconnect();

            Assert.IsFalse(m_S3Client.IsConnected);
        }

        [Test]
        [ExpectedException(ExceptionType = typeof(System.Security.Authentication.AuthenticationException))]
        public void TestWrongKey()
        {
            m_S3Client = new S3Client(ACCESS_KEY, SECRET_KEY, false);

            m_S3Client.SecretKey = "abc";
            // connect
            bool success = m_S3Client.EstablishConnection();

            Assert.IsTrue(success);
            Assert.IsTrue(m_S3Client.IsConnected);

            // disconnect
            m_S3Client.Disconnect();

            Assert.IsFalse(m_S3Client.IsConnected);
        }

        [Test]
        public void TestBuckets()
        {
            m_S3Client = new S3Client(ACCESS_KEY, SECRET_KEY);

            // connect
            bool success = m_S3Client.EstablishConnection();

            foreach (var item in m_S3Client.Buckets)
            {
                Assert.IsNotEmpty(item.Name);
            }

            // disconnect
            m_S3Client.Disconnect();
        }

        [Test]
        public void TestCreateBucket()
        {
            m_S3Client = new S3Client(ACCESS_KEY, SECRET_KEY);

            // connect
            bool success = m_S3Client.EstablishConnection();

            Bucket.Create(m_S3Client, "clientTest2");

            var b = (from a in m_S3Client.Buckets
                     where a.Name == "clientTest2"
                     select a);

            Assert.IsNotNull(b);

            // disconnect
            m_S3Client.Disconnect();
        }
    }
}
